package com.android.jingweiapp.fragment.publicinfo;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.GetJGOfficeXMLPhase;
import com.android.jingweiapp.XMLPhase.GetZSDepXMLPhase;
import com.android.jingweiapp.activity.DetailTextActivity;
import com.android.jingweiapp.adapter.NormalRecyclerViewAdapter;
import com.android.jingweiapp.bean.OrganizationBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 机构设置
 */
public class OrganizationFragment extends Fragment {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.btn1)
    TextView btn1;
    @Bind(R.id.btn2)
    TextView btn2;

    private ArrayList<Parcelable> beans;
    private NormalRecyclerViewAdapter adapter;


    public OrganizationFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_organization, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        beans = new ArrayList<>();
        adapter = new NormalRecyclerViewAdapter(getActivity(), beans, Contacts.TYPE_ADAPTER.TYPE_ORGAN);
        recycleView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recycleView.setAdapter(adapter);


        btn1.setSelected(true);
        new MyAsyncTask1().execute();

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyAsyncTask1().execute();
                btn1.setSelected(true);
                btn2.setSelected(false);
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyAsyncTask2().execute();
                btn1.setSelected(false);
                btn2.setSelected(true);
            }
        });


    }

    private void dealData(String content) {
        Gson gson = new Gson();
        OrganizationBean bean = gson.fromJson(content, OrganizationBean.class);
        beans.clear();
        beans.addAll(bean.getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    class MyAsyncTask1 extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetJGOfficeXMLPhase.createParam(getActivity()),
                    new GetJGOfficeXMLPhase());
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            adapter.setOnItemClickListener(new NormalRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, Parcelable data) {
                    startActivity(new Intent(getActivity(), DetailTextActivity.class)
                            .putExtra(Contacts.KEY.KEY_OBJECT, data)
                            .putExtra(Contacts.KEY.KEY_TYPE, Contacts.TYPE.ORGAN_INFO_OFFICE));
                }
            });
            dealData(s);
        }
    }

    class MyAsyncTask2 extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetZSDepXMLPhase.createParam(getActivity()),
                    new GetZSDepXMLPhase());
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            adapter.setOnItemClickListener(new NormalRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, Parcelable data) {
                    startActivity(new Intent(getActivity(), DetailTextActivity.class)
                            .putExtra(Contacts.KEY.KEY_OBJECT, data)
                            .putExtra(Contacts.KEY.KEY_TYPE, Contacts.TYPE.ORGAN_INFO_DEP));
                }
            });
            dealData(s);
        }
    }
}
